﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

namespace DAL
{
    public class ShoppingDal : IDAL.IShoppingDal
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delall(string id)
        {
            List<MODEL.Shopping> list = RedisHelper.Get<List<MODEL.Shopping>>("showshopping");
            string[] s = id.Split(',');
            foreach (var item in s)
            {
                if (list.Remove(list.Where(m => m.sid == Convert.ToInt32(item)).FirstOrDefault()))
                {

                }
            }
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                string str = "delete from shopping where sid in ('" + id + "')";
                SqlCommand com = new SqlCommand(str, con);
                int i = com.ExecuteNonQuery();
                return i;
            }
        }
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int add(MODEL.Shopping model)
        {
            List<MODEL.Shopping> listshopping = null;
            listshopping.Add(model);
            RedisHelper.Set("showshopping", listshopping);
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                string str = "insert into shopping values('" + model.goodsname + "','" + model.image + "'," + model.price + "," + model.num + ")";
                SqlCommand com = new SqlCommand(str, con);
                int i = com.ExecuteNonQuery();
                return i;
            }
        }
        /// <summary>
        /// 显示购物车
        /// </summary>
        /// <returns></returns>
        public List<MODEL.Shopping> show()
        {
            List<MODEL.Shopping> list = RedisHelper.Get<List<MODEL.Shopping>>("showshopping");
            return list;
            //List<MODEL.Shopping> listshopping = RedisHelper.Get<List<MODEL.Shopping>>("showshopping");
            //if(listshopping==null||listshopping.Count==0)
            //{
            //    using (SqlConnection con = new SqlConnection(constr))
            //    {
            //        string str = "select * from shopping";
            //        SqlDataAdapter da = new SqlDataAdapter(str, con);
            //        DataTable dt = new DataTable();
            //        da.Fill(dt);
            //        var list2 = JsonConvert.SerializeObject(dt);
            //        var list = JsonConvert.DeserializeObject<List<MODEL.Shopping>>(list2);
            //        RedisHelper.Set("showshopping", list);
            //        return list;
            //    }
            //}
            //else
            //{
            //    return listshopping;
            //}
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int del(int id)
        {
            List<MODEL.Shopping> list = RedisHelper.Get<List<MODEL.Shopping>>("showshopping");
            if (list.Remove(list.Where(m => m.sid == id).FirstOrDefault()))
            {

            }
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                string str = "delete from shopping where sid=" + id;
                SqlCommand com = new SqlCommand(str, con);
                int i = com.ExecuteNonQuery();
                return i;
            }
        }
        public MODEL.BaseModel updid(int id)
        {
            throw new NotImplementedException();
        }

        public int upd(MODEL.Shopping model)
        {
            throw new NotImplementedException();
        }
    }
}
